var customerBag = new Array(); //客户集合，用于保存已选择的客户
$j(document).ready(function () {
	//验证
	applyValidate($("lowestShopPoint"),"integer");
	applyValidate($("highestShopPoint"),"integer");
	
	searchCustomers(true);
});

//重设搜索条件
function resetSearchCriteria(){
	fnResetForm("customerSearchCriteria");
	$j('#customerSearchCriteria input[name="regionId"]').val("");//重置注册地区
}


//搜索产品 isNew标示是否新的搜索,param标示是否自定义参数搜索
function searchCustomers(isNew,param){
	var postData;
	var url;
	if(isNew){
		if(null==param){
			if(validateSearchCriteria()){
				//有搜索结果搜索
			    postData = $j('#customerSearchCriteria :input').serializeArray();
			    url=__ctxPath+"/customer/customerSelector.html?doAction=search&decorator=blank";
				$j("#customerSelectorResults").load(url,postData,searchCustomersCallback);   
		    }else{
		    	return;
		    }
		}else{
			//新的搜索。全部，上架等
			if(param.membershipId  != undefined){
				$j('#membershipId').val(""+param.membershipId);
			}
		    postData = $j('#customerSelectorResults :input').serializeArray();
			url=__ctxPath+"/customer/customerSelector.html?doAction=search&decorator=blank&&btnSearch=1";
			$j("#customerSelectorResults").load(url,postData,searchCustomersCallback);  
		}    
	}else{
		postData = $j('#customerSelectorResults :input').serializeArray();
		url=__ctxPath+"/customer/customerSelector.html?doAction=search&decorator=blank";
		$j("#customerSelectorResults").load(url,postData,searchCustomersCallback);  
	} 
	
}

//分页
function fnOnGoToPage(){
	searchCustomers(false);
}

//搜索产品回调函数
function searchCustomersCallback(){
	if(multiple)$j("#multipleSelectDiv").show();
}
//验证函数
function validateSearchCriteria(){
	if(!validateForm($j('#customerSearchCriteria').get(0))){
    	return false;
   	}
   	return true;
}
//选择多个客户
function selectCustomers(){
	if(customerBag.length == 0){
		alert(__FMT.customerSelector_pleaseSelect);
		return;
	}
	callbackFunc.call(this,customerBag);
	cleanupCustomers();
}
//清空客户
function cleanupCustomers(){
	customerBag = new Array();
	$j("#selectedCustomerInfo").empty();
}

//选择客户
function selectCustomer(customerId){
	var customer = {"customerId":customerId,
					"username":$('username_'+customerId).value,
					"fullname":$('fullname_'+customerId).value};
					
	if(multiple){
		addCustomer(customer);
	}else{
		callbackFunc.call(this,customer);
	}
}
//添加客户
function addCustomer(customer){
	
	if(!isContain(customer.customerId)){
		customerBag.push(customer);
		addCustomerInfo(customer);
	}else{
		jAlert(__FMT.customerSelector_alreadySelect);
	}	
}
//移除客户
function removeCustomer(customerId){
	var index = indexOfCustomerBag(customerId);
	if(-1 != index){
		customerBag.remove(index);
		removeCustomerInfo(customerId);
	}
}
//获取customer在customerbag中的index，不存在返回-1
function indexOfCustomerBag(customerId){
	for(var i=0; i<customerBag.length; i++){
		var item = customerBag[i];
		if(item.customerId == customerId){
			return i;
		}
	}
	return -1;
}
//判断是否已经包含该客户
function isContain(customerId){
	var isExist = false;
	for(var i=0; i<customerBag.length; i++){
		var item = customerBag[i];
		if(item.customerId == customerId){
			isExist = true;
			break;
		}
	}
	return isExist;
}
//添加客户显示信息 customer是一个JSONObject{customerId,usernmae,fullname}
function addCustomerInfo(customer){
	var html = '';
	html += '<li id="customerSelectedInfo'+customer.customerId+'"><div class="first" style="width:170px;">'+customer.username+'</div>';
	html += '<div class="second"><a href="javascript:void(0)" class="del" onclick="removeCustomer('+customer.customerId+')">&nbsp;</a></div>';    
	html += '</li>';
	$j("#selectedCustomerInfo").append(html);
	
}
//移除客户显示信息
function removeCustomerInfo(customerId){
	$j("#customerSelectedInfo"+customerId).remove();
}


/*
   *  方法:Array.remove(index)
   *  功能:删除数组元素.
   *  参数:index删除元素的下标.
   *  返回:在原数组上修改数组
   */

 Array.prototype.remove=function(index)
 {
   if(isNaN(index)||index>this.length){return false;}
   for(var i=0,n=0;i<this.length;i++)
   {
       if(this[i]!=this[index])
       {
           this[n++]=this[i]
       }
   }
   this.length-=1
 }
 

